<template>
  <div :class="info && info.type ? '' : 'app-container'">
    <div class="page-filter mb10 fl">
      <el-form ref="filter" :model="filter" label-width="0">
        <el-form-item class="mr10 mb5 fl">
          <SupplierSelect v-model="filter.sup_id" />
        </el-form-item>
        <el-form-item class="mr10 mb5 fl">
          <ProFilter v-model="filter.pro_id"/>
        </el-form-item>
        <el-form-item class="mr10 mb5 fl">
          <el-input v-model="filter.num" placeholder="发票号码" size="small" clearable />
        </el-form-item>
        <el-form-item class="mr10 mb5 fl">
          <el-date-picker
            v-model="filter.begin_invoice_time"
            placeholder="开始时间"
            type="date"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            size="small"
            clearable
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item class="mr10 mb5 fl">
          <el-date-picker
            v-model="filter.end_invoice_time"
            placeholder="结束时间"
            type="date"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            size="small"
            clearable
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item class="mr10 mb5 fl" style="width: 300px">
          <el-button size="small" @click="app.resetFields($refs['filter'].$parent,'filter')">清 空</el-button>
          <el-button type="primary" size="small" @click="getList">查 询</el-button>
          <el-button
            type="primary"
            size="small"
            @click="handleDialog({
              dTitle: '新增发票',
              dType: 'add',
              dField: dField
            })"
          >新 增</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table ref="multipleTable" class="search-table" :data="list" border 
      :summary-method="getSummaries"
      show-summary
      fit highlight-current-row style="width: 100%;">
      <el-table-column label="序号" align="center" width="56px">
        <template slot-scope="{$index}">
          {{ $index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="归属单位" width="120px" prop="company" />
      <el-table-column label="项目名称" width="220px" prop="pro_name" />
      <el-table-column label="PO单号" width="190px">
        <template slot-scope="{row}">
          {{ 'PO'+row.pro_id }}
        </template>
      </el-table-column>
      <el-table-column label="供应商" width="220px" prop="sup_name" />
      <el-table-column label="合同编号" width="190px">
        <template slot-scope="{row}">
          {{ 'LOPO'+row.sn }}
        </template>
      </el-table-column>
      <el-table-column label="发票号码" width="190px" prop="num" />
      <el-table-column label="未税金额" width="110px" align="right" prop="no_tax" />
      <el-table-column label="税额" width="110px" align="right" prop="tax" />
      <el-table-column label="含税金额" width="110px" align="right" prop="has_tax" />
      <el-table-column label="税率" width="110px" align="right" prop="rate" />
      <el-table-column label="发票类别" width="120px" prop="type" />
      <el-table-column label="开票日期" width="110px" align="center" prop="invoice_time" />
      <el-table-column label="收票日期" width="110px" align="center" prop="collect_time" />
      <el-table-column label="录入人员" width="110px" prop="staff_name" />
      <el-table-column label="备注" min-width="260px" prop="remark" />
      <el-table-column label="操作" align="center" width="90px" fixed="right">
        <template slot-scope="{row}">
          <el-dropdown>
            <el-button type="primary" size="mini">
              操作<i class="el-icon-arrow-down el-icon--right" />
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                @click.native="handleDialog({...row, ...
                {dTitle: '编辑发票', dType: 'edit', dField: dField}})"
              >
                编 辑
              </el-dropdown-item>
              <el-dropdown-item
                @click.native="files({e_id: row.id, type: '',
                                      db_type: 'logc_pro_invoice_archives'})"
              >
                附 件
              </el-dropdown-item>
              <el-dropdown-item @click.native="handleDelete(row)">删 除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="filter.pageAll>0" :total="filter.pageAll" :page.sync="filter.pageCurrent" :limit.sync="filter.pageNumber" @pagination="getList" />
    <Cos ref="cos" :info="dialog" />
    <Dialog ref="dialog" :get-data="getList" :info="dialog" />
  </div>
</template>

<script>
import { puris, selPurs, delPurchaseInvoice } from '@/api/purchase'
import Dialog from './components/invoice/invoice-dialog'
import SupplierSelect from '@/myComponents/SupplierSelect'
import ProFilter from '@/components/ProFilter'
import page from '@/utils/page'

export default {
  name: '',
  components: { Dialog, SupplierSelect, ProFilter },
  extends: page,
  props: {
    reqList: {
      default: puris,
      type: Function
    },
    reqDel: {
      default: delPurchaseInvoice,
      type: Function
    },
    info: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      prices: [0, 0, 0],
      sles: [],
      dField: ['id', 'company', 'pur_id', 'no_tax', 'tax', 'has_tax',
        'rate', 'type', 'invoice_time', 'collect_time', 'remark', 'num']
    }
  },
  created() {
    if (this.$route.query.pur_id) {
      this.filter.pur_id = this.$route.query.pur_id
      const o = {}
      o.key = this.$route.query.pur_id
      o.value = this.$route.query.sn
      this.sles.push(o)
    }
    this.getList()
  },
  methods: {
    selPurs(query) {
      let str = ''
      if (Object.prototype.toString.call(query) === '[object String]') str = query
      selPurs({ sn: str }).then(response => {
        this.sles = response.list
      }).catch(err => {
        this.$message.error(err)
      })
    },
    getListAfter(e) {
      this.prices[0] = e.summary.no_tax
      this.prices[1] = e.summary.tax
      this.prices[2] = e.summary.has_tax
    },
    getSummaries() {
      const sums = []
      sums[0] = '合计'
      sums[7] = this.app.toFix(this.prices[0], 2)
      sums[8] = this.app.toFix(this.prices[1], 2)
      sums[9] = this.app.toFix(this.prices[2], 2)
      return sums
    }
  }
}
</script>
